package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public Result add(CheckItem checkItem) {
        try {
            checkItemDao.add(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    @Override
    public PageResult selectAllByName(QueryPageBean queryPageBean) {
        Long count = (long)checkItemDao.selectCount(queryPageBean.getQueryString());
        if (queryPageBean.getCurrentPage()<=0){
            queryPageBean.setCurrentPage(1);
        }
        int totalPage = (int) (count%queryPageBean.getPageSize()==0?count/queryPageBean.getPageSize():count/queryPageBean.getPageSize()+1);
        if (queryPageBean.getCurrentPage()>=totalPage){
            queryPageBean.setCurrentPage(totalPage);
        }
        List<CheckItem> checkItemList = new ArrayList<>();
        if (queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()>0&&!queryPageBean.getQueryString().equals("null")){
            checkItemList = checkItemDao.selectByCondition(queryPageBean.getQueryString());
        }else {
            int start = (queryPageBean.getCurrentPage()-1)*queryPageBean.getPageSize();
            checkItemList =  checkItemDao.selectAllByPage(start,queryPageBean.getPageSize());
        }

        return new PageResult(count,checkItemList);
    }

    @Override
    public Result deleteById(int id) {
       int count =  checkItemDao.selectCountById(id);
       if (count>0){
           return new Result(false,"删除失败,您没有权限删除哦!");
       }
        try {
            checkItemDao.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @Override
    public Result updateOen(CheckItem checkItem) {
        try {
            checkItemDao.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    @Override
    public List<CheckItem> findAll() {
       return checkItemDao.findAll();
    }
}
